<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h2>ajax+form-插件</h2>
    <!-- 
      action 提交的接口地址
      method 提交的方法
     -->
    <form action="http://ajax-api.itheima.net/api/data" method="post">
      <!-- name 提交到服务器数据的 key -->
      <input
        class="username"
        name="username"
        type="text"
        placeholder="请输入用户名"
      />
      <br />
      <input
        class="food"
        name="food"
        type="text"
        placeholder="请输入喜欢的食物"
      />
      <br />
      <input
        class="sign"
        name="sign"
        type="textarea"
        placeholder="请输入个性签名"
      />
      <br />
      <input
        class="male"
        name="male"
        type="textarea"
        placeholder="请输入性别"
      />
      <br />
      <input
        class="male"
        name="hobby"
        type="textarea"
        placeholder="请输入爱好"
      />
      <br />
      <input class="male" name="age" type="textarea" placeholder="请输入年龄" />
      <br />
      <button type="submit">提交</button>
    </form>
    <script src="./lib/axios.js"></script>
    <!-- 1. 导入插件 -->
    <script src="./lib/form-serialize.js"></script>
    <script>
      // http://ajax-api.itheima.net/api/data
      axios.defaults.baseURL='http://ajax-api.itheima.net'
      document.querySelector('form').onsubmit = (e) => {
        e.preventDefault()
        const data = serialize(document.querySelector('form'), { hash: false })
        axios({
          method: 'post',
          url: '/api/data',
          data,
        }).then((res) => {
          console.log(res)
        })
      }
    </script>
  </body>
</html>
